package example.dao.provider;

import example.pojo.ShipInfo;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class ShipInfoDynaSqlProvider {
    public String selectWithParam(final Map<String, Object> params) {
    String sql = new SQL() {
        {
            SELECT("*");
            FROM("ship_info");
            if (params.get("shipInfo") != null) {
                ShipInfo shipInfo = (ShipInfo) params.get("shiptInfo");
                if (shipInfo.getCode() != null && !"".equals(shipInfo.getCode())) {
                    WHERE(" code = #{shipInfo.code} ");
                }
                if (shipInfo.getName() != null && !shipInfo.getName().equals("")) {
                    WHERE("  name LIKE CONCAT ('%',#{shipInfo.name},'%') ");
                }
                if (shipInfo.getName() != null && !shipInfo.getName().equals("")) {
                    WHERE("  name LIKE CONCAT ('%',#{shipInfo.name},'%') ");
                }

            }
        }
    }.toString();
		if (params.get("pager") != null) {
        sql += " limit #{pager.firstLimitParam} , #{pager.perPageRows}  ";
    }
		return sql;
}

    // 鏍规嵁鏉′欢鍔ㄦ�佹煡璇㈠晢鍝佹�昏褰曟暟
    public String count(final Map<String, Object> params) {
        return new SQL() {
            {
                SELECT("count(*)");
                FROM("product_info");
                if (params.get("productInfo") != null) {
                    ShipInfo shipInfo = (ShipInfo) params.get("productInfo");
                    if (shipInfo.getCode() != null && !"".equals(shipInfo.getCode())) {
                        WHERE(" code = #{productInfo.code} ");
                    }
                    if (shipInfo.getName() != null && !shipInfo.getName().equals("")) {
                        WHERE("  name LIKE CONCAT ('%',#{productInfo.name},'%') ");
                    }

                }
            }
        }.toString();
    }
}
